﻿/*
 *  PirateShare - a tool for fair&legal content sharing among friends.  
 *  see <http://pirateshare.codeplex.com> for latest version.
 *
 *  Copyright (C) 2010 Ilya Elenskiy
 *
 *  This program is free software; you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation; either version 2 of the License, or 
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License 
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PirateShare
{
    [Serializable]
    public class LocalSharedFile : SharedFile
    {
        public string FullFilename { get; private set; }
        public DateTime LastModified { get; private set; }

        public LocalSharedFile(string name) : base(name)
        {
            this.FullFilename = name;
            FileInfo fi = new FileInfo(name);
            this.LastModified = fi.LastWriteTimeUtc;
        }


        public SharedFileSegment GetSegment(long SegmentID)
        {
            FileStream theFile = new FileStream(FullFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
            long offset = SegmentID * Globals.FileSegmentSize;
            theFile.Seek(offset, SeekOrigin.Begin);

            BinaryReader reader = new BinaryReader(theFile);
            byte[] buffa = reader.ReadBytes(Globals.FileSegmentSize);
                        

            SharedFileSegment resSegment = new SharedFileSegment(this, this.Hash, SegmentID, buffa);
            return resSegment;
        }
    }
}
